import threading
import queue
import time

end = False


class GetListHtml(threading.Thread):

    def __init__(self, url_que: queue.Queue):
        super().__init__()
        self.url_que = url_que

    def run(self):
        try:
            for i in range(30):
                time.sleep(2)
                print('获取到详情页')
                self.url_que.put(f'www.baidu.com/{i}')
        finally:
            global end
            end = True


class GetDetailHtml(threading.Thread):

    def __init__(self, url_que: queue.Queue):
        super().__init__()
        self.url_que = url_que

    def run(self):
        while True:
            try:
                url = self.url_que.get(timeout=2)
                print(f'请求详情页{url}-{threading.currentThread().getName()}')
                time.sleep(1)
            except queue.Empty:
                global end
                if end:
                    break


if __name__ == '__main__':
    url_queue = queue.Queue(100)
    t1 = GetListHtml(url_queue)
    t2 = GetDetailHtml(url_queue)
    t3 = GetDetailHtml(url_queue)

    t1.start()
    t2.start()
    t3.start()
